/*
 * PurchaseBean.java
 *
 * Created on 13 novembre 2007, 14:38
 */
package com.reimscopie.beans.entities;

import java.util.* ;

import javax.persistence.* ;

import com.reimscopie.* ;

/**
 * This entity represents a purchase. This is NOT the cart.
 */
@Entity
public class PurchaseBean extends ValueBean {
    
  // the quantity of the purchased product 
  private int quantity ;
  
  // TODO: OPTIONS D'ACHAT
  
  // The product of this purchase
  private ProductBean product ;
  
  
  /**
   * Create a new instance of <code>PurchaseBean</code> (default constructor).
   */
  public PurchaseBean() {
  }
  
  
  /**
   * Create a new instance of <code>PurchaseBean</code> with all attributes.
   *
   * @param purchase The valued purchase.
   * @param product The product bean.
   */
  public PurchaseBean(Purchase purchase,ProductBean product) {
    
    if (purchase.hasId())
      throw new IllegalArgumentException("purchase has an image") ;
    
    this.product  = product ;
    this.quantity = purchase.getQuantity() ;
    
  }
  
  
  /**
   * Return the quantity for this product.
   *
   * @return The quantity of the product.
   */
  public int getQuantity() {
    return quantity ;
  }
  
  
  /**
   * Set the quantity for the associated product.
   *
   * @param quantity The new quantity.
   */
  public void setQuantity(int quantity) {
    this.quantity = quantity ;
  }
  
  /**
   * Return the product of this purchase.
   *
   * @return All products.
   */
  @OneToOne
  public ProductBean getProduct() {
    return product ;
  }
  
  
  /**
   * Set the product of this purchase.
   *
   * @param product The product of this purchase.
   */
  public void setProduct(ProductBean product) {
    this.product = product ;
  }
  
////////////////////////////////////////////////////////////////////////////////
//                           TRANSFOMATION                                    //
////////////////////////////////////////////////////////////////////////////////

  /**
   * Transforms this entity bean into the associated purchase.
   *
   * @return A new purchase instance.
   *
   * @see #toValue
   */
  public Purchase toPurchase() {
    return toValue() ;
  }

  
  /**
   * Transform this entity bean into the associated value.
   * 
   * @return A new value instance.
   */
  @Override
  public Purchase toValue() {
    
    return new Purchase(
        getId(),
        product.toProduct(),
        getQuantity()
    ) ;
    
  }
  
}
